﻿using System.Data;
using Ledict.ConditionExtract.Model;

namespace Ledict.ConditionExtract.BLL.WorkProcess
{
    /// <summary>
    /// 排序错误后，重新处理
    /// </summary>
    public class WorkThread_Order : WorkThread
    {

        public int MaxCount = 0;//处理总数
        private int CurrentCount = 0;//处理当前数量
        

        protected TreeDataBLL trbll = new TreeDataBLL();
        protected Express_WordBLL wordbll = new Express_WordBLL();
        protected Express_AddElement addelementbll = new Express_AddElement();
        protected ExpressGroupSituationBLL expresitu = new ExpressGroupSituationBLL();
        protected Express_MatchSituationBLL match = new Express_MatchSituationBLL();
        protected AboutRegexBLL aboutreg = new AboutRegexBLL();
        protected RegexParse.RegexParse getregex = new RegexParse.RegexParse();
        protected ProcessDataSourceBLL datas = new ProcessDataSourceBLL();
        protected WorkThread_Public work = new WorkThread_Public();
        protected XmlLabel_LabelRelBLL xmlLabel_LabelRelBLL = new XmlLabel_LabelRelBLL();
        CaseCauseBLL casecausebll = new CaseCauseBLL();
        StaticCacheBLL staticcache = new StaticCacheBLL();



        #region 加工处理(核心)

        /// <summary>
        /// 开始加工（一个案由）
        /// </summary>
        public override void execute_Start()
        {
           
            //1、要改变排序的数据
            DataTable CaseCausedt;
            CaseCausedt = datas.GetDataSourceAllCaseCause();
            for (int i = 0; i < CaseCausedt.Rows.Count; i++)
            {
                MaxCount += datas.GetDataSource_WrongOrder(CaseCausedt.Rows[i]["CC_ID"].ToString(), 0).Rows.Count;
            }
            // 没数据结束进度条
            if (MaxCount == 0)   //没有可以跑数据的案例
            {
                if (MaxCount >= CurrentCount)
                {
                    processShow(MaxCount, CurrentCount);
                }
            }
            else//加工数据
            {
                execute_order();
            }

        }


        public void execute_order()//案由ID
        {
            string ParagraphText = "";
            DataTable conditiondt=new DataTable ();//下级
            DataTable CaseCausedt;
            DataTable orderdt;
            CaseCausedt = datas.GetDataSourceAllCaseCause();
            for (int i = 0; i < CaseCausedt.Rows.Count; i++)
            {
                //找出所有的小分类的排序不对的部分
                orderdt = datas.GetDataSource_WrongOrder(CaseCausedt.Rows[i]["CC_ID"].ToString(),1);
                if (orderdt.Rows.Count>0)
                conditiondt = staticcache.GetHeadwordData(CaseCausedt.Rows[i]["CC_ID"].ToString());
                //整合顺序
                for (int o = 0; o < orderdt.Rows.Count; o++)
                {
                    ParagraphText = orderdt.Rows[o]["ParagraphText"].ToString();
                    SYS_Express_AgainInfomation ModelOrder = new Model.SYS_Express_AgainInfomation();
                    ModelOrder.EW_ID = orderdt.Rows[o]["Id"].ToString();
                    ModelOrder.CaseCauseID = orderdt.Rows[o]["CaseCauseID"].ToString();
                    ModelOrder.ES_SituationIds = orderdt.Rows[o]["ES_SituationIds"].ToString();
                    ModelOrder.ParagraphTypeId = orderdt.Rows[o]["ParagraphTypeId"].ToString();
                    ModelOrder.CaseId = orderdt.Rows[o]["CaseId"].ToString();
                    //做
                    execute_Order(conditiondt, ParagraphText, ModelOrder);
                    #region 处理完成后,进度条

                    CurrentCount++; // 显示进度条
                    if (MaxCount >= CurrentCount)
                    {
                        processShow(MaxCount, CurrentCount);
                    }
                    lockObj.WaitOne();

                    #endregion
                }
                if (orderdt.Rows.Count > 0)
                casecausebll.UpCaseCause_IsAgainProcessData(CaseCausedt.Rows[i]["CC_ID"].ToString());

            }


        }

        public void execute_Order(DataTable conditiondt, string inputText, SYS_Express_AgainInfomation model)//第一层情形情节（conditiondt限定规则范围）
        {
            model.ES_SituationIds = work.OrderCC_IDList(model.ES_SituationIds, conditiondt);
            work.AddToExpress(inputText, model.CaseId, model.ParagraphTypeId, model.CaseCauseID, model.ES_SituationIds);

        }

        #endregion

    }









}
